package com.haiyou.mini.programs.config;

import com.haiyou.common.util.RedisUtil;
import com.haiyou.mini.programs.coupon.merchant.MerchantCouponService;
import com.haiyou.mini.programs.coupon.merchant.impl.MerchantCouponServiceImpl;
import com.haiyou.mini.programs.properties.WxMiniProgramProperties;
import com.haiyou.mini.programs.service.WxMiniAppService;
import com.haiyou.mini.programs.service.WxMiniPayNotifyService;
import com.haiyou.mini.programs.service.WxMiniPayService;
import com.haiyou.mini.programs.service.WxUploadService;
import com.haiyou.mini.programs.service.impl.WxMiniAppServiceImpl;
import com.haiyou.mini.programs.service.impl.WxMiniPayNotifyServiceImpl;
import com.haiyou.mini.programs.service.impl.WxMiniPayServiceImpl;
import com.haiyou.mini.programs.service.impl.WxUploadServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自动配置
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(value = WxMiniProgramProperties.class)
@ConditionalOnProperty(prefix = WxMiniProgramProperties.PREFIX, name = "enable", havingValue = "true")
public class WxMiniAutoConfiguration {

    @Bean
    public RedisUtil redisUtil() {
        return new RedisUtil();
    }

    @Bean
    public WxMiniConfig wxPayConfig(WxMiniProgramProperties properties) {
        WxMiniConfig wxPayConfig = new WxMiniConfig();
        BeanUtils.copyProperties(properties, wxPayConfig);
        return wxPayConfig;
    }

    /**
     * 支付接口
     *
     * @param properties
     * @return
     */
    @Bean
    public WxMiniPayService wxMiniPayService(WxMiniProgramProperties properties) {
        log.info("---->小程序支付配置properties={}", properties);
        return new WxMiniPayServiceImpl(wxPayConfig(properties));
    }

    /**
     * 支付回调接口
     *
     * @param properties
     * @return
     */
    @Bean
    public WxMiniPayNotifyService wxMiniPayNotifyService(WxMiniProgramProperties properties) {
        log.info("---->小程序支付回调配置properties={}", properties);
        return new WxMiniPayNotifyServiceImpl(wxPayConfig(properties));
    }

    /**
     * 服务接口
     *
     * @param properties
     * @return
     */
    @Bean
    public WxMiniAppService wxMiniLoginService(WxMiniProgramProperties properties) {
        log.info("---->小程序服务配置properties={}", properties);
        return new WxMiniAppServiceImpl(wxPayConfig(properties), redisUtil());
    }

    /**
     * 图片上传接口
     *
     * @param properties
     * @return
     */
    @Bean
    public WxUploadService wxUploadService(WxMiniProgramProperties properties) {
        return new WxUploadServiceImpl(wxPayConfig(properties));
    }

    /**
     * 商家劵服务接口
     *
     * @param properties
     * @return
     */
    @Bean
    public MerchantCouponService merchantCouponService(WxMiniProgramProperties properties) {
        return new MerchantCouponServiceImpl(wxPayConfig(properties));
    }
}
